package edu.xhu;

import com.constant.Constant;
import com.net.ClientError;
import com.net.LoadData;
import com.net.RequestListener;
import com.net.RequestParameter;

import edu.xhu.util.ClientActivity;
import edu.xhu.view.LoadingDialog;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class FeedbackActivity extends Activity{

	private Button mButtonBack;
	private EditText feedBackEditText;
	private Button mButtonSubmit;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feedback);
		
		initView();
		setListener();
	}
	
	private void initView(){
		mButtonBack			= (Button) findViewById(R.id.button_back);
		feedBackEditText	= (EditText) findViewById(R.id.edittext_feedback);
		mButtonSubmit		= (Button) findViewById(R.id.button_submit);
	}
	
	private void setListener(){
		mButtonBack.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
				
				/*Intent intent = new Intent();
				intent.setClass(FeedbackActivity.this,XiHuaActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);*/
			}
		});
		
		mButtonSubmit.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String content = feedBackEditText.getText().toString().trim();
				if (content == null || content.equals("")){
					Animation shake = AnimationUtils.loadAnimation(FeedbackActivity.this, R.anim.shake);
					feedBackEditText.startAnimation(shake);
					return;
				}
				
				LoadingDialog.showWindow(FeedbackActivity.this);
				RequestParameter parameter = new RequestParameter();
				XhuApplication application = (XhuApplication) getApplication();
				parameter.add("imei", ((XhuApplication)getApplication()).getImei());
				parameter.add("content", feedBackEditText.getText().toString());
				LoadData.loadData(Constant.FEEDBACK, parameter, new RequestListener()
				{
					
					public void onError(ClientError ed)
					{
						LoadingDialog.dismiss();
						ErrorShow.showError(FeedbackActivity.this, ed);
					}
					
					public void onComplete(Object obj)
					{
						LoadingDialog.dismiss();
						if(obj.toString().equals("true"))
						{
							Toast.makeText(FeedbackActivity.this, "谢谢你的反馈，我们将及时处理反馈信息", 0).show();
							finish();
						}
						else
						{
							Toast.makeText(FeedbackActivity.this, "反馈信息失败", 0).show();
						}
					}
				});
			}
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK){
			if (LoadingDialog.isShowing())
				LoadingDialog.dismiss();
			else {
				finish();
				
				/*Intent intent = new Intent();
				intent.setClass(FeedbackActivity.this,XiHuaActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);*/
			}
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}
}
